package io.renren.modules.admin.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import io.renren.modules.admin.entity.MyCourseEntity;
import io.renren.modules.admin.service.MyCourseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.Arrays;
import java.util.List;
import java.util.Map;


/**
 * 购买课程
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2019-12-17 18:23:59
 */
@RestController
@RequestMapping("admin/mycourse")
@Api("用户课程")
public class MyCourseController {
    @Autowired
    private MyCourseService myCourseService;

    /**
     * 列表
     */
    @ApiIgnore
    @RequestMapping("/list")
    @RequiresPermissions("admin:mycourse:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = myCourseService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @ApiOperation("获取 用户绑定的课程")
    @GetMapping("/info/{userId}")
   // @RequiresPermissions("admin:mycourse:info")
    public R info(@PathVariable("userId") Long userId){
		//MyCourseEntity myCourse = myCourseService.getById(myCourseId);
        QueryWrapper<MyCourseEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",userId);
        List<MyCourseEntity> list = myCourseService.list(wrapper);

        return R.ok().put("myCourses", list);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
//    @RequiresPermissions("admin:mycourse:save")
    @ApiOperation("保存 用户绑定课程")
    public R save(@RequestBody List<MyCourseEntity> myCourses){

        //删除用户之前绑定课程
        QueryWrapper<MyCourseEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",myCourses.get(0).getUserId());
        myCourseService.remove(wrapper);

        //批量保存
        myCourseService.saveBatch(myCourses);
        return R.ok();
    }

    /**
     * 修改
     */
    @ApiIgnore
    @RequestMapping("/update")
    @RequiresPermissions("admin:mycourse:update")
    public R update(@RequestBody MyCourseEntity myCourse){
		myCourseService.updateById(myCourse);

        return R.ok();
    }

    /**
     * 删除
     */
    @ApiIgnore
    @RequestMapping("/delete")
    @RequiresPermissions("admin:mycourse:delete")
    public R delete(@RequestBody Long[] myCourseIds){
		myCourseService.removeByIds(Arrays.asList(myCourseIds));

        return R.ok();
    }

}
